<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_V2_Order extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Order
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Order
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('order', $this->request_params);

        // 服务
        $this->service = new Service_Api_Order();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 订单列表
    public function action_list()
    {
        try {
            $this->validation->list()->check();

            $this->result['data'] = $this->service->list($this->mid, $this->request_params);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {

            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 订单详情
    public function action_detail()
    {
        try {
            $this->validation->detail()->check();
            $order = Model_Member_Order::get_order_detail($this->request_params['id'], $this->mid);
            if (empty($order)) {
                throw new Exception('订单不存在');
            }
            $service              = Service_Api_Member_Order_Factory::get_instance($order['typeid']);
            $this->result['data'] = $service->detail($this->mid, $this->request_params['id'], $order);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获得游客信息
    public function action_get_traveller_list()
    {
        try {
            $this->validation->get_traveller_list()->check();

            $this->result['data'] = $this->service->get_traveller_list($this->mid, $this->request_params['id']);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 取消订单
    public function action_cancel_order()
    {
        try {
            $this->validation->cancel_order()->check();

            $this->result['data'] = $this->service->cancel_order($this->mid, $this->request_params['ordersn']);
            $this->result['msg']  = '取消成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
            $this->result['trace']    = $e->getTrace();
        }
    }

    // 确认消费
    public function action_consume_order()
    {
        try {
            $this->validation->consume_order()->check();

            $this->result['data'] = $this->service->consume_order($this->mid, $this->request_params['id']);
            $this->result['msg']  = '处理成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 确认收货
    public function action_express_confirm()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->express_confirm()->check();

            $this->result['data'] = $this->service->express_confirm($this->mid, $this->request_params['id']);
            $this->result['msg']  = '确认成功';
            $db->commit();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();

            $db->rollback();
        }
    }

    // 获得合同信息
    public function action_get_contract_content()
    {
        try {
            $this->validation->get_contract_content()->check();

            $this->result['data'] = $this->service->get_contract_content($this->mid, $this->request_params['id']);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获得订单状态
    public function action_get_status()
    {
        try {
            $this->validation->get_status()->check();

            $this->result['data'] = $this->service->get_status($this->mid, $this->request_params['id']);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获得物流进度
    public function action_get_express_progress()
    {
        try {
            $this->validation->get_express_progress()->check();

            $this->result['data'] = $this->service->get_express_progress($this->mid, $this->request_params['ordersn']);
            $this->result['msg']  = '获取成功';
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

}